<template>
  <div class="table-all">
    <!-- 搜索区 -->
    <div class="app-container search-box">
      <SearchForm
        :form="searchData.form"
        :options="searchData.options"
        @searchUpdate="search"
        @resetSearchData="resetSearchData"
      ></SearchForm>
    </div>
    <!-- 按钮区 -->
    <div class="app-container table-box">
      <div class="table-main">
        <!-- 表格 -->
        <v-table
          title="预警处置时限配置信息"
          v-if="tableMainHeight>0"
          :table-columns="tableColumns"
          :table-data="tableData"
          :loading="loading"
          :is-index="true"
          ref="table"
          :maxHeight="tableMainHeight">
          <template #topButtonList>
            <ButtonForm :buttonList="buttonList" @clickButton="clickButton"></ButtonForm>
          </template>
          <template #modeType="scope">
            {{getModeType(scope.row.modeType)}}
          </template>
          <template #oneLimit="scope">
            {{getLimitStr(scope.row.dealJson,1)}}
          </template>
          <template #twoLimit="scope">
            {{getLimitStr(scope.row.dealJson,2)}}
          </template>
          <template #threeLimit="scope">
            {{getLimitStr(scope.row.dealJson,3)}}
          </template>
          <template #fourLimit="scope">
            {{getLimitStr(scope.row.dealJson,4)}}
          </template>
          <template #operation="scope">
            <a v-hasPermission="['alarmDealConfig:add']" class="table-btn-edit table-operation"
               @click="handleEdit(scope.row)">{{$t('table.edit')}}</a>
            <a v-hasPermission="['alarmDealConfig:delete']" class="table-btn-delete table-operation"
               @click="singleDelete(scope.row)">{{$t('table.delete')}}</a>
          </template>
        </v-table>
      </div>
      <div class="table-page">
        <!-- 页码 -->
        <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize"
                    @pagination="search"/>
      </div>
    </div>
    <!-- 详情 -->
    <detailDialog :dialog-visible="detailDialogShow"
                  @success="editSuccess"
                  @close="editClose"
                  :openType="detailOpenType"
                  :modeTypeFormatOptions = "modeTypeFormatOptions"
                  :typeOptions = "typeOptions"
                  :limitUnitOptions = "limitUnitOptions"
                  :id="detailId"
                  :title="detailTitle"
    ></detailDialog>
  </div>
</template>

<script>
  import { alarmDealConfigPage,
    alarmDealConfigExport,
    alarmDealConfigDelete } from '@/api/alarm/dealConfig'
  import SearchForm from '@/components/searchForm/index'
  import ButtonForm from '@/components/ButtonForm/index'
  import VTable from '@/components/VTable/index'
  import Pagination from '@/components/Pagination/index'
  import detailDialog from './detail.vue'
  export default {
    components:{
      VTable,Pagination,detailDialog,SearchForm,ButtonForm
    },
    data(){
      return{
        tableColumns:[
          {
            prop: 'code',
            label: '标识码',
            minWidth: '150',
            slotname: 'code',
            options: {}
          },
          {
            prop: 'modeType',
            label: '预警类型',
            minWidth: '150',
            slotname: 'modeType',
            options: {}
          },
          {
            prop: 'oneLimit',
            label: '一级预警处置时限',
            minWidth: '150',
            slotname: 'oneLimit',
            options: {}
          },
          {
            prop: 'twoLimit',
            label: '二级预警处置时限',
            minWidth: '150',
            slotname: 'twoLimit',
            options: {}
          },
          {
            prop: 'threeLimit',
            label: '三级预警处置时限',
            minWidth: '150',
            slotname: 'threeLimit',
            options: {}
          },
          {
            prop: 'fourLimit',
            label: '四级预警处置时限',
            minWidth: '150',
            slotname: 'fourLimit',
            options: {}
          },
          {
            prop: 'remark',
            label: '备注',
            minWidth: '150',
            slotname: 'remark',
            options: {}
          },
          {
            prop: 'createBy',
            label: '创建人',
            minWidth: '150',
            slotname: 'createBy',
            options: {}
          },
          {
            prop: 'createTime',
            label: '创建时间',
            minWidth: '150',
            slotname: 'createTime',
            options: {}
          },
          {
            prop: 'operation',
            label: '操作',
            width: '200',
            align: 'center',
            slotname: 'operation',
            overflow: false,
            fixed: 'right'
          }
        ],
        tableData:[],
        selection: [],
        loading:false,
        total:1,
        pageNum:1,
        pageSize:10,
        detailOpenType:"add",
        detailDialogShow:false,
        detailId:"",
        detailTitle:"新增",
        dict:{},
        searchData:{
          form:{
            code:'',
            modeType:'',
            remark:'',
          },
          options:[
            {
              title:'标识码',
              key: "code",
              type: "text",
            },
            {
              title:'预警类型',
              key: "modeType",
              type: "cascader",
              multiple:false,
              option: {
                data: this.modeTypeFormatOptions,
                props: {
                  label: "label", // 指定label对应data的字段
                  value: "value", // 指定value对应data的字段
                },
              },
            },
            {
              title:'备注',
              key: "remark",
              type: "text",
            },
          ]
        },
        buttonList:[
          {name:'新增',methods:'handleAdd',type:'primary', hasPremission:["alarmDealConfig:add"]},
          {name:'导出数据',methods:'handleExport',type:'primary', hasPremission:["alarmDealConfig:export"]}
        ],
        tableMainHeight: 0,
        modeTypeOptions:[],
        modeTypeFormatOptions:[],
        typeOptions:[{label:"不限制",value:0},{label:"限制", value:1}],
        limitUnitOptions:[{label:"小时", value:1},{label:"天", value:2}],
      }
    },
    watch: {
      // 字典
      'modeTypeOptions':{
        handler(newVal, oldVal) {
          this.modeTypeFormatOptions = newVal.map(t=>{
            let name = JSON.parse(t.name)
            return { label: t.label, value: name.value, children: name.children}
          })
        },
        deep: true,
        immediate: true
      },
      'modeTypeFormatOptions':{
        handler(newVal, oldVal) {
          this.searchData.options.forEach(t=>{
            if(t.key=='modeType'){
              t.option.data = newVal
            }
          })
        },
        deep: true,
        immediate: true
      }
    },
    methods:{
      getLimitStr(val, level){
        // [{"levelLabel":"一级报警","level":1,"type":1,"limitUnit":1,"limitNum":"24"},{"levelLabel":"二级报警","level":2,"type":0,"limitUnit":"","limitNum":""},{"levelLabel":"三级报警","level":3,"type":0,"limitUnit":"","limitNum":""}]
        if(val){
          let value = JSON.parse(val)
          let item = value.find(t=>t.level==level)
          if(undefined!=item){
            let typeStrItem =  this.typeOptions.find(t=> t.value == item.type)
            if(item.type==0){
              return typeStrItem.label
            }
            return typeStrItem.label+","+ item.limitNum + this.limitUnitOptions.find(t=> t.value == item.limitUnit).label
          }
        }
      },
      getModeType(value){
        let type = value.split(",")
        let item = this.modeTypeFormatOptions.find(t=>t.value== type[0])
        if(undefined!=item){
          console.log("item====>",item)
          let subItem = item.children.find(t=>t.value == type[1])
          if(undefined!=subItem){
            return subItem.label
          }
        }
        return;
      },
      getCheckBoxValue(value,option){
        if(!value){
          return
        }
        let temp = []
        let data = value.split(",")
        data.forEach(t=>{
          let item = option.find(x=> x.value == t)
          if(item != undefined){
            temp.push(item.label)
          }
        })
        return temp.join(",")
      },
      getDictValue(value,option){
        let item = option.find(t=> t.value == value)
        if(item!=undefined){
          return item.label
        }
        return value
      },
      editClose(){
        this.detailDialogShow = false
      },
      editSuccess(){
        this.search()
        this.detailDialogShow = false
      },
      search(){
        this.fetch({
          ...this.searchData.form,
        })
      },
      fetch(params={}){
        params.pageSize = this.pageSize
        params.pageNum = this.pageNum
        if(params.modeType){
          params.modeType = params.modeType.toString()
        }
        // （1报警 2预警）
        params.type = 2
        this.loading = true
        alarmDealConfigPage(params).then(res=>{
          if(res.code==200){
            this.total = res.data.total
            this.tableData = res.data.rows
          }else{
            this.$message({
              message: this.$t('tips.systemException'),
              type: 'error'
            })
          }
        }).finally(final=>{
          this.loading = false
        })
      },
      // 重置搜索条件
      resetSearchData(){
        this.searchData.form = {
          code:'',
          type:'',
          modeType:'',
          remark:'',
        }
      },

      // 触发按钮区域按钮方法
      clickButton(methods){
        this[methods]()
      },
      handleAdd(){
        this.detailOpenType = "add"
        this.detailTitle = "新增"
        this.detailDialogShow = true
      },
      handleEdit(row){
        this.detailId = row.id
        this.detailOpenType = "edit"
        this.detailTitle="编辑"
        this.detailDialogShow = true
      },
      handleExport(){
        // （1报警 2预警）
        let params = {
          ...this.searchData.form,
          type: 2
        }
        alarmDealConfigExport(params,`预警处置时限配置_${new Date().getTime()}.xlsx`);
      },
      handleImport(){
        console.log("等待处理")
      },
      closeDialog(){
        this.detailDialogShow = false
      },

      singleDelete(row){
        this.$confirm(this.$t('tips.confirmDelete'), this.$t('common.tips'), {
          confirmButtonText: this.$t('common.confirm'),
          cancelButtonText: this.$t('common.cancel'),
          type: 'warning'
        }).then(() => {
          this.loading = true
          alarmDealConfigDelete(row.id).then(res=>{
            if(res.code==200){
              this.$message({
                message: this.$t('tips.deleteSuccess'),
                type: 'success'
              })
              this.search()
            }else{
              this.$message({
                message: this.$t('tips.systemException'),
                type: 'error'
              })
            }
          }).finally(final=>{
            this.loading = false
          })
        })
      }
    },
    activated(){
      this.fetch()
    },
    created(){
      // 获取字典值
      this.$commonJs.filterDataDict("alarmDealLimitType",this.modeTypeOptions)
    },
    mounted(){
      this.tableMainHeight = document.getElementsByClassName('table-main')[0].clientHeight - 44 //减去顶部按钮列表的高度
    }
  }
</script>

<style lang="scss" scoped>
  .table-all {
    display: flex;
    flex-direction: column;
    height: 100%;

    .search-box {
      padding-bottom: 6px;
    }

    .table-box {
      padding-top: 10px;
      flex: 1;
      display: flex;
      flex-direction: column;

      .table-main {
        flex: 1;
        overflow: auto;
      }

      .table-page {
        // padding-top: 10 px;
        // padding-bottom: 10 px;
      }

      ::v-deep .pagination-container {
        text-align: right;
      }

    }
  }
</style>
